angular.module('mycontrollers', [])
	.controller('ContentController', function($scope, $ionicSideMenuDelegate) {

		$scope.toggleLeft = function() {
			$ionicSideMenuDelegate.toggleLeft();
		};
	})
	.controller('registerCtrl', ['$scope', '$location', function($scope, $location) {
		Bmob.initialize("b895f09906b8044d63943c06a053dcf5", "9774ab2974b0203f6011f81dab299ddc");
		$scope.username = "";
		$scope.passwd = "";
		$scope.register = function() {
			if ($scope.username == null || $scope.username.length == 0 || $scope.passwd == null || $scope.passwd.length == 0) {
				alert("账号密码不能为空！");
				return;
			} else {
				var User = Bmob.Object.extend("User");
				var user = new User();
				user.set("username", $scope.username);
				user.set("password", $scope.passwd);
				user.save(null, {
					success: function(object) {

						$location.path('/login');
					},
					error: function(model, error) {
						alert("create object fail");
					}
				});
			};
		};
	}])
	.controller('loginCtrl', ['$scope', '$location', function($scope, $location) {
		Bmob.initialize("b895f09906b8044d63943c06a053dcf5", "9774ab2974b0203f6011f81dab299ddc");
		$scope.username = [];
		$scope.passwd = [];
		$scope.goin = function() {
			if ($scope.username == null || $scope.username.length == 0 || $scope.passwd == null || $scope.passwd.length == 0) {
				alert("账号密码不能为空！");
				return;
			} else {
				console.log($scope.username + "");
				console.log($scope.passwd + "");
				var User = Bmob.Object.extend("User");
				var query = new Bmob.Query(User);
				query.equalTo("username", $scope.username);
				query.find({
					success: function(results) {
						if (results.length != 0) {
							var object = results[0];
							alert(object.get('password'));
							if ($scope.passwd == object.get('password')) {
							alert("sdasd");
								$location.path('/goin');
							}
						};
					},
					error: function(error) {
						alert('账号或密码错误');
					}
				});
			}

		};
	}]);